myApp.controller('managerAdminListCtrl',function($scope,$serMaganerAdmin,$uibModal,$log,toaster,$localStorage) {

    $scope.vm = {
        data: {},
        page: {}
    };

    $scope.model = {
        save:function(state,data){
            if (state == 0) {
                $scope.model.info = {
                    token : $localStorage.userInfo&&$localStorage.userInfo.token,
                    state:state,
                    name:"",
                    info:"",
                    status:false,
                    sort:1
                };
            }
            if (state == 1 && data) {
                $scope.model.info = {
                    token : $localStorage.userInfo&&$localStorage.userInfo.token,
                    id:data.id,
                    state:state,
                    name:data.name,
                    info:data.info,
                    status:data.status==1?true:false,
                    sort:data.sort
                };
            }
            var modalInstance = $uibModal.open({
                templateUrl: 'managerAdminModal.html',
                controller: 'managerAdminItemModalSaveCtrl',
                windowClass: 'managerAdminItemSave',
                resolve: {
                    info: function () {
                        return $scope.model.info;
                    }
                }
            });
            modalInstance.result.then(function (res) {
                init();
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        },
        del:function(id){
            var modalInstance = $uibModal.open({
                template: '<div class="modal-header"><h3 class="modal-title">确定要删除吗？</h3></div>' +
                '<div class="modal-footer"><button class="btn btn-default" ng-click="cancel()">Cancel</button>' +
                '<button class="btn btn-primary" ng-click="ok()">OK</button></div>',
                controller: 'managerAdminItemModalDelCtrl',
                windowClass: 'managerAdminItemDel',
                size: 'sm'
            });
            modalInstance.result.then(function (res) {
                var type='';
                if(res){
                    $serMaganerAdmin.del({
                        token : $localStorage.userInfo&&$localStorage.userInfo.token,
                        id:id
                    }).then(function(res){
                        switch (res.code){
                            case 1:type = 'success';break;
                            default:type = 'error';break;
                        }
                        toaster.pop(type, '信息', res.message);
                        init($scope.vm.key);
                    })
                }
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        }
    };

    /*跳转页面*/
    $scope.pageChanged = function() {
        init($scope.vm.page.currentpage-1);
    };
    /*每页显示数量显示*/
    $scope.placement = {
        options: [5,10,20,50],
        pageSize:5,
        selected: 10,
        changeFn:function(){
            init(0,0);
        }
    };
    /*初始化数据*/
    function init(page){
        $serMaganerAdmin.list({
            token : $localStorage.userInfo&&$localStorage.userInfo.token,
            page: page||0,
            limit: $scope.placement.selected
        }).then(function (res) {
            $scope.vm.data = res.data;
            if(!page||page==0){$scope.vm.page = res.addtion_data;}
        });
    }
    init();
});

myApp.controller('managerAdminItemModalDelCtrl', ['$scope', '$uibModalInstance', function($scope,$uibModalInstance) {
    $scope.ok = function () {
        $uibModalInstance.close(true);
    };
    $scope.cancel = function () {
        $uibModalInstance.dismiss(0);
    };
}]);

myApp.controller('managerAdminSaveCtrl', function($scope,$serMaganerAdmin,$serMaganerNodeAdmin,toaster,$stateParams,$localStorage) {
    $scope.state = $stateParams.id==0?false:true;
    if($stateParams.id!=0){
        $serMaganerAdmin.detail({
            token : $localStorage.userInfo&&$localStorage.userInfo.token,
            id:$stateParams.id
        }).then(function (res) {
            $scope.info = res.roleInfo;
            $scope.info.status = res.roleInfo.status==1?true:false;
            $scope.node = res.node;
        });
    }else{
        $scope.info = {
            token : $localStorage.userInfo&&$localStorage.userInfo.token,
            name:'',
            status:false,
            info:''
        };
        $serMaganerNodeAdmin.tree({
                token : $localStorage.userInfo&&$localStorage.userInfo.token,
            }
        ).then(function (res) {
            $scope.node = res.data;
        });
    }

    $scope.changeFn = function(v,s,c){
        if(s){   //控制器
            angular.forEach(v.functionNode,function(m,n){
                m.select = v.select;
            })
        }else {  //方法
            var jishu = 0;
            angular.forEach(c.functionNode,function(m,n){
                if(m.select){
                    jishu++;
                }
            });
            c.select = jishu>0?true:false;
        }
    };

    $scope.ok = function (state) {
        if(!$scope.info.name){toaster.info('警告','名字不能为空');return false;}
        if(!$scope.info.info){toaster.info('警告','备注不能为空');return false;}

        var nodeArr  = [];

        angular.forEach($scope.node,function(v,i){
            if(!!v.select){
                nodeArr.push(v.id)
            }
            angular.forEach(v.functionNode,function(m,n){
                if(!!m.select){
                    nodeArr.push(m.id)
                }
            })
        });

        if(state){/*修改*/
            $serMaganerAdmin.editorRole({
                token : $localStorage.userInfo&&$localStorage.userInfo.token,
                id:$scope.info.id,
                name:$scope.info.name,
                info:$scope.info.info,
                status:$scope.info.status==true?1:0,
                sort:$scope.info.sort
            }).then(function (res) {
                if(res.code==1){
                    $serMaganerAdmin.editorNode({
                        token : $localStorage.userInfo&&$localStorage.userInfo.token,
                        id:$scope.info.id,
                        nodeInfo:nodeArr
                    }).then(function(res){
                        if(res.code==1){
                            toaster.success('成功',res.message);
                            history.go(-1);
                            return false;
                        }else {
                            toaster.error('错误',res.message);
                        }
                    })
                }else {
                    toaster.error('错误',res.message);
                }
            })
        }else {/*添加*/
            $serMaganerAdmin.add({
                token : $localStorage.userInfo&&$localStorage.userInfo.token,
                name:$scope.info.name,
                info:$scope.info.info,
                status:$scope.info.status==true?1:0,
                sort:$scope.info.sort
            }).then(function (res) {
                if(res.code==1){
                    $serMaganerAdmin.editorNode({
                        token : $localStorage.userInfo&&$localStorage.userInfo.token,
                        id:res.data,
                        nodeInfo:nodeArr
                    }).then(function(res){
                        if(res.code==1){
                            toaster.success('成功',res.message);
                            history.go(-1);
                            return false;
                        }else {
                            toaster.error('错误',res.message);
                        }
                    })
                }else {
                    toaster.error('错误',res.message);
                }
            })
        }
    };
});